#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
    string reverseWords(string s)
    {
        string ans = "";
        s += " ";
        int sLen = s.size();
        string::size_type pos;
        for (int i = 0; i < sLen; i++)
        {
            pos = s.find(" ", i);
            if (pos < sLen)
            {
                string word = s.substr(i, pos - i);
                for (int i = word.length() - 1; i >= 0; i--)
                {
                    ans += word[i];
                }
                ans += " ";
                i = pos;
            }
        }
        return ans.erase(ans.find_last_not_of(" ") + 1);
    }

    string reverseWords_2(string s)
    {
        int sLen = s.size();
        if (sLen == 0)
        {
            return "";
        }
        int i = 0;
        while (i < sLen)
        {
            int start = i;
            while (i < sLen && s[i] != ' ')
            {
                i++;
            }
            int left = start, right = i - 1;
            while (left < right)
            {
                swap(s[left], s[right]);
                left++;
                right--;
            }
            while (i < s.size() && s[i] == ' ')
            {
                i++;
            }
        }
        return s;
    }

    string reverseWords_3(string s)
    {
        string ret;
        int length = s.length();
        int i = 0;
        while (i < length)
        {
            int start = i;
            while (i < length && s[i] != ' ')
            {
                i++;
            }
            for (int p = start; p < i; p++)
            {
                ret.push_back(s[start + i - 1 - p]);
            }
            while (i < length && s[i] == ' ')
            {
                i++;
                ret.push_back(' ');
            }
        }
        return ret;
    }
};
int main()
{
    Solution s;
    string str = "Let's take LeetCode contest";
    cout << s.reverseWords(str) << endl;
    system("pause");
    return 0;
}
